<?php

namespace App\Controllers;

use App\Services\SearchTimeLimitService;
use common\enum\CommonEnum;
use Illuminate\Http\Request;
use common\utils\SysCode;
use App\Services\PayChannelService;
use App\Services\ReportMoneyLogService;

/**
 * Class CashController
 * @package App\Controllers
 */
class CashController extends BaseController
{
    // public $allows= ['getCashTransferList','getHallList','getGameList','getCashTipsList'];
    public function getGameList(Request $request){
        $ret = ReportMoneyLogService::getGameList();
        return $ret;
    }
    public function getHallList(Request $request)
    {
        $ret = ReportMoneyLogService::getHallList();
        return $ret;
    }
    // 出入款汇总
    public function getCashTransferList(Request $request)
    {
        $params = $request->input();
        //搜索时间限制
        if (!empty($params['date_begin']) && !empty($params['date_end'])) {
            SearchTimeLimitService::timeLimit($params['date_begin'],$params['date_end'],SearchTimeLimitService::SIX_MONTH);
        }
        $ret = ReportMoneyLogService::getCashTransferList($params);
        if($ret && $ret['list']){
            $ret = amount_format_array($ret, array_merge(ReportMoneyLogService::getSqlKeys(),['transfer_diff_balance','send_balance']));
            $keys = array_filter(array_keys($ret['list'][0]), function ($val) {
                return substr($val, -4) == '_num';
             });
            $len = count($ret['list']);
            foreach($ret['list'] as $k => &$data){
                foreach($keys as $v){
                    //小计和总计两行数据不需要显示人数
                    if($k < $len - 2){
                        $dataKey = substr($v, 0, -4) . '_amount';
                        $data[$dataKey] = sprintf("%s (%s人)", $data[$dataKey], $data[$v]);
                    }
                    unset($data[$v]);
                }
            }
        }
        return $ret;
    }
    //红包小费
    public function getCashTipsList(Request $request){
        $params = $request->input();
        //搜索时间限制
        if (!empty($params['date_begin']) && !empty($params['date_end'])) {
            SearchTimeLimitService::timeLimit($params['date_begin'],$params['date_end'],SearchTimeLimitService::SIX_MONTH);
        }
        $ret = ReportMoneyLogService::getCashTipsList($params);
        if($ret){
            amount_format_array($ret,['tips_amount','redbag_amount']);
        }
        return $ret;
    }
}
